package com.jinzhi.jzweb.controller.moduleConfig;


import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.controller.yyrby.ScResultVO;
import com.jinzhi.jzweb.domain.ExaminationResultsComDO;
import com.jinzhi.jzweb.pojo.ExaminationResultsPoVo;
import com.jinzhi.jzweb.pojo.ExaminationResultsVO;
import com.jinzhi.jzweb.pojo.moduleConfig.OverallStatisticsVo;
import com.jinzhi.jzweb.service.ExaminationResultsComService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 成绩总统计
 * </pre>
 * <small> 2023-05-05 15:11:09 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationAnswerSheetComZongtongji")
@Api(value = "实操居家场景分数统计", tags = {"实操居家场景分数统计"})
public class ExaminationAnswerSheetComZongtongjiController extends BaseController {

    @Autowired
    private ExaminationResultsComService examinationResultsService;

    @Autowired
    private ExaminationResultsComService examinationResultsComService;


    /**
     * 成绩总统计导出
     *
     * @param response
     */
    @ApiOperation(value = "成绩总统计导出", notes = "成绩总统计导出")
    @GetMapping("/downloadImportStu")
    @RequiresPermissions("jzweb:examinationAnswerSheetComZongtongji:import")
    @ResponseBody
    public void downloadImportTemplate(HttpServletResponse response, ExaminationResultsComDO examinationAnswerSheetComDT) {

        examinationResultsService.overallStatisticsDownloadImport(response,examinationAnswerSheetComDT);
    }

    /**
     * 超级管理员 一级管理员查看分数
     * @param model
     * @return
     */
    @GetMapping()
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComZongtongji")
    String info( Model model) {
        return "jzweb/examinationAnswerSheetComZongtongji/examinationAnswerSheetComZongtongji";
    }

    /**
     * 成绩总统计
     * @param examinationAnswerSheetComDTO
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComZongtongji")
    public Result<Page<OverallStatisticsVo>> list(ExaminationResultsComDO examinationAnswerSheetComDTO){

        return Result.ok(examinationResultsService.overallStatistics(examinationAnswerSheetComDTO));
    }




    /**
     * 成绩总统计最高分最低分
     * @return
     */
    @ResponseBody
    @GetMapping("/listMaxAndMin")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComZongtongji")
    public Result<Map> listMaxAndMin(){

        List<ExaminationResultsVO> examinationResultsVOPage = examinationResultsComService.overallStatistics(1, 9999).getRecords();
        if (examinationResultsVOPage.size()==0){
            Map rstMap =new HashMap();
            rstMap.put("max","0");
            rstMap.put("min","0");
            rstMap.put("size","0");
            return Result.ok(rstMap);
        }
        String[] maxAndMin = getMaxAndMin(examinationResultsVOPage);
        Map rstMap =new HashMap();
        rstMap.put("max",maxAndMin[0]);
        rstMap.put("min",maxAndMin[1]);
        rstMap.put("size",examinationResultsVOPage.size()*2);
        return Result.ok(rstMap);
    }


    /**
     * 获取最高分最低分
     * @param scoreList
     * @return
     */
    public String[] getMaxAndMin(List<ExaminationResultsVO> scoreList) {
        String[] rst = new String[2];
        // 初始化最高分和最低分为第一个考生的分数
        double highestScore = Double.parseDouble(scoreList.get(0).getTeamScore());
        double lowestScore = Double.parseDouble(scoreList.get(0).getTeamScore());

        // 遍历集合，找出最高分和最低分
        for (ExaminationResultsVO student : scoreList) {
            if (Double.valueOf(student.getTeamScore()) > highestScore) {
                // 更新最高分
                highestScore = Double.valueOf(student.getTeamScore());
            }
            if (Double.valueOf(student.getTeamScore()) < lowestScore) {
                // 更新最低分
                lowestScore = Double.valueOf(student.getTeamScore());
            }
        }
        rst[0] = String.valueOf(highestScore); //最高分
        rst[1] = String.valueOf(lowestScore); //最低分
        return  rst;
    }







    /**
     * 成绩公式表导出
     *
     * @param response
     */
    @ApiOperation(value = "成绩公式表导出", notes = "成绩公式表导出")
    @GetMapping("/documentWord")
    @RequiresPermissions("jzweb:examinationAnswerSheetComZongtongji:documentWord")
    @ResponseBody
    public void documentWord(HttpServletResponse response, ExaminationResultsComDO examinationAnswerSheetComDT) throws Exception {

        examinationResultsService.documentWord(response,examinationAnswerSheetComDT);
    }


}
